package com.fosun.spr.plugin.learn.api.v2User2tbshop.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.learn.api.v2User2tbshop.dao.V2User2tbshopDao;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.V2User2tbshopService;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.DeleteSpModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.DeleteSrModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.GetSpModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.GetSrModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.ListSpModel;
import com.fosun.spr.plugin.learn.api.v2User2tbshop.service.model.ListSrModel;
import com.fosun.spr.plugin.learn.helper.mybatis.dao.V2User2tbshopMapper;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2User2tbshop;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2User2tbshop;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2User2tbshopExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class V2User2tbshopServiceImpl extends CoBaseService implements
		V2User2tbshopService {
	@Autowired 
	private V2User2tbshopDao v2User2tbshopDao;
	@Autowired
	private V2User2tbshopMapper v2User2tbshopMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(v2User2tbshopDao.list(spModel));
		srModel.setTotalCount(v2User2tbshopDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		V2User2tbshop db = v2User2tbshopMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"帐号2店铺-v2_user2tbshop");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		V2User2tbshop db = v2User2tbshopMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		v2User2tbshopMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getUserId(),"用户");
		ExtAsserts.notNull(spModel.getTbshopId(),"淘宝店");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		
		//if(StringUtils.isBlank(spModel.getId())){
		if(spModel.getId()==null){
			
			V2User2tbshop db = new V2User2tbshop();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			//db.setId(getUuid());
			v2User2tbshopMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			
			V2User2tbshop db = v2User2tbshopMapper.selectByPrimaryKey(spModel.getId());
			db.setUserId(spModel.getUserId());
			db.setTbshopId(spModel.getTbshopId());
			v2User2tbshopMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
